package ru.myrtle.umf.codegen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class GenModel<ModelType, ContextType> {

    private final String name;
    private final Collection<GenRule<ModelType, ContextType>> rules =
            new ArrayList<GenRule<ModelType, ContextType>>();

    /*
     *
     */

    public GenModel(String name, GenRule<ModelType, ContextType>... rules) {
        this.name = name;
        this.rules.addAll(
                Arrays.asList(rules)
        );
    }

    /*
     *
     */

    public void generate(ModelType model, ResourceSet resourceSet, GenMonitor monitor, ContextType context)
            throws GenException {
        
        if (monitor == null) {
            monitor = GenMonitor.NULL;
        }

        monitor.modelStarted(
                name, resourceSet.getDestination().getAbsolutePath(), resourceSet.getSuffix()
        );

        for (GenRule<ModelType, ContextType> rule : rules) {
            rule.generate(
                    model, resourceSet, monitor, context
            );
        }

        monitor.modelFinished();
    }
}
